package com.cz.modules.rll.controller;

import java.text.SimpleDateFormat;
import java.util.*;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cz.common.validator.ValidatorUtils;
import com.cz.modules.rll.entity.RllPlaceEntity;
import com.cz.modules.rll.service.RllPlaceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cz.modules.rll.entity.RllHourCountEntity;
import com.cz.modules.rll.service.RllHourCountService;
import com.cz.common.utils.PageUtils;
import com.cz.common.utils.R;



/**
 * 
 *
 * @author Cz
 * @email 
 * @date 2020-04-26 20:25:43
 */
@RestController
@RequestMapping("rll/rllhourcount")
public class RllHourCountController {
    @Autowired
    private RllHourCountService rllHourCountService;
    @Autowired
    private RllPlaceService rllPlaceService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("rll:rllhourcount:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = rllHourCountService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("rll:rllhourcount:info")
    public R info(@PathVariable("id") String id){
        RllHourCountEntity rllHourCount = rllHourCountService.getById(id);

        return R.ok().put("rllHourCount", rllHourCount);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("rll:rllhourcount:save")
    public R save(@RequestBody RllHourCountEntity rllHourCount){
        rllHourCountService.save(rllHourCount);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("rll:rllhourcount:update")
    public R update(@RequestBody RllHourCountEntity rllHourCount){
        ValidatorUtils.validateEntity(rllHourCount);
        rllHourCountService.updateById(rllHourCount);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("rll:rllhourcount:delete")
    public R delete(@RequestBody String[] ids){
        rllHourCountService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
    /**
     * 客流量统计 第二页面
     * type 0 本天 1 本月
     */
    @RequestMapping("/kllChartbytime")
    public R kllChartbytime(@RequestParam Map<String, Object> params){
        Date date=new Date();
        String type= (String) params.get("type");
        String time= (String) params.get("time");
        String startTime="";
        String endTime="";
        if(!"".equals(time) && time !=null){//有自定义时间
            if("0".equals(type)){//该自定义时间 的当天的起止时间的数据
                startTime =time+" 00:00:00";
                endTime =time+" 23:59:59";
            }else if("1".equals(type)){//该自定义时间 的当月的起止时间的数据
                startTime =time+"-01 00:00:00";
                Date dd=DateUtil.parse(startTime);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(dd);
                calendar.add(Calendar.MONTH, 1);
                calendar.set(Calendar.DAY_OF_MONTH, 0);
                endTime = DateUtil.format(calendar.getTime(),"yyyy-MM-dd");
                endTime +=" 23:59:59";
            }
        }else {//无自定义时间
            if("0".equals(type)){//当天的数据
              /*  Calendar calendar = Calendar.getInstance();
              // calendar.add(Calendar.HOUR,-1);
                endTime=DateUtil.format(calendar.getTime(),"yyyy-MM-dd HH");
                endTime+=":00:00";
                startTime=DateUtil.format(date,"yyyy-MM-dd");
                startTime +=" 00:00:00";*/

                startTime= DateUtil.format(new Date(),"yyyy-MM-dd");
                startTime+=" 00:00:00";
                endTime=DateUtil.format(new Date(),"yyyy-MM-dd HH");
                endTime+=":00:00";
            }else if("1".equals(type)){//当月的数据
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                // 获取前月的第一天
                calendar.add(Calendar.MONTH, 0);
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                startTime= DateUtil.format(calendar.getTime(),"yyyy-MM-dd");
                startTime +=" 00:00:00";
                endTime = DateUtil.format(date,"yyyy-MM-dd");
                endTime +=" 23:59:59";
            }
        }
        if("0".equals(type)){//小时
            if("".equals(time) && time !=null){
                List slist=rllHourCountService.kllChart1("南区",startTime,endTime);
                List nlist=rllHourCountService.kllChart1("北区",startTime,endTime);
                return R.ok().put("time",slist.get(0)).put("skll",slist.get(1)).put("nkll",nlist.get(1));
            }else {
                List slist=rllHourCountService.kllCharthour("南区",startTime,endTime);
                List nlist=rllHourCountService.kllCharthour("北区",startTime,endTime);
                return R.ok().put("time",slist.get(0)).put("skll",slist.get(1)).put("nkll",nlist.get(1));
            }

        }else if("1".equals(type)){//天
            List slist=rllHourCountService.kllChartday("南区",startTime,endTime);
            List nlist=rllHourCountService.kllChartday("北区",startTime,endTime);
            return R.ok().put("time",slist.get(0)).put("skll",slist.get(1)).put("nkll",nlist.get(1));
        }
        return R.ok();
    }


    /**
     * 24小时客流量统计
     * type 0 24小时 1 7天 30天
     */
    @RequestMapping("/kllChart/{type}")
    public R kllChart(@PathVariable("type")String type){
        List slist=rllHourCountService.kllChart("南区",type);
        List nlist=rllHourCountService.kllChart("北区",type);
        return R.ok().put("time",slist.get(0)).put("skll",slist.get(1)).put("nkll",nlist.get(1));
    }

    //今日客流量统计+本小时
    @RequestMapping("/kllChart1/{type}")
    public R kllChart1(@PathVariable("type")String type){
        String startTime="";
        String endTime="";
        if("0".equals(type)){
            startTime= DateUtil.format(new Date(),"yyyy-MM-dd");
            startTime+=" 00:00:00";
            endTime=DateUtil.format(new Date(),"yyyy-MM-dd HH");
            endTime+=":00:00";
        }
        List southList=rllHourCountService.kllChart1("南区",startTime,endTime);
        List northList=rllHourCountService.kllChart1("北区",startTime,endTime);
        return R.ok().put("southList",southList).put("northList",northList);
    }
    /**
     *卡口人流量对比
     *
     */
    @RequestMapping("/peoplecount")
    public R peoplecount(@RequestParam Map<String, Object> params){
        String type= (String) params.get("type");
        String time= (String) params.get("time");
        Date date=new Date();
        String startTime="";
        String endTime="";
        if(!"".equals(time) && time !=null){//有自定义时间
            if("0".equals(type)){//该自定义时间 的当天的起止时间的数据
                startTime =time+" 00:00:00";
                endTime =time+" 23:59:59";
            }else if("1".equals(type)){//该自定义时间 的当月的起止时间的数据
                startTime =time+"-01 00:00:00";
                Date dd=DateUtil.parse(startTime);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(dd);
                calendar.add(Calendar.MONTH, 1);
                calendar.set(Calendar.DAY_OF_MONTH, 0);
                endTime = DateUtil.format(calendar.getTime(),"yyyy-MM-dd");
                endTime +=" 23:59:59";
            }
        }else {//无自定义时间
            if("0".equals(type)){//当天的数据
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                calendar.add(Calendar.HOUR_OF_DAY, 1);//1小时后
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);
                endTime=DateUtil.format(calendar.getTime(),"yyyy-MM-dd HH:mm:ss");
                startTime=DateUtil.format(date,"yyyy-MM-dd");
                startTime +=" 00:00:00";
            }else if("1".equals(type)){//当月的数据
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                // 获取前月的第一天
                calendar.add(Calendar.MONTH, 0);
                calendar.set(Calendar.DAY_OF_MONTH, 1);
                startTime= DateUtil.format(calendar.getTime(),"yyyy-MM-dd");
                startTime +=" 00:00:00";
                // 获取当前月的最后一天
                Calendar calendar1 = Calendar.getInstance();
                calendar1.setTime(date);
                calendar1.add(Calendar.MONTH, 1);
                calendar1.set(Calendar.DAY_OF_MONTH, 0);
                endTime = DateUtil.format(calendar1.getTime(),"yyyy-MM-dd");
                endTime +=" 23:59:59";

            }
        }
        return R.ok().put("peoplecountlist",rllHourCountService.peoplecount(startTime,endTime));
    }
    /**
     * 昨天，本月 客流量
     */
    @RequestMapping("/rllCountHp")
    public R rllCountHp(){
        List<RllPlaceEntity> southList=rllPlaceService.list(new QueryWrapper<RllPlaceEntity>().eq("area","南区").eq("type","0"));
        List<RllPlaceEntity> northList=rllPlaceService.list(new QueryWrapper<RllPlaceEntity>().eq("area","北区").eq("type","0"));
        String startTime="";
        String endTime="";
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(new Date());
        calendar1.add(Calendar.DAY_OF_MONTH, -1);
        String  yesterday= DateUtil.format(calendar1.getTime(),"yyyy-MM-dd");
        startTime =yesterday+" 00:00:00";
        endTime =yesterday+" 23:59:59";
        Integer scounty=rllHourCountService.kllCount(startTime,endTime,southList);
        Integer ncounty=rllHourCountService.kllCount(startTime,endTime,northList);

        String datemonth= DateUtil.format(new Date(),"yyyy-MM");
        startTime =datemonth+"-01 00:00:00";
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 0);
        endTime = DateUtil.format(calendar.getTime(),"yyyy-MM-dd");
        endTime +=" 23:59:59";
        Integer scountm=rllHourCountService.kllCount(startTime,endTime,southList);
        Integer ncountm=rllHourCountService.kllCount(startTime,endTime,northList);
        return R.ok().put("southy",scounty).put("northy",ncounty).put("zongy",scounty+ncounty).put("southm",scountm).put("northm",ncountm).put("zongm",scountm+ncountm);
    }
}
